package com.tomehay.termprojectmanage_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tomehay.termprojectmanage_server.enums.ResponseCode;
import com.tomehay.termprojectmanage_server.mapper.ClazzMapper;
import com.tomehay.termprojectmanage_server.mapper.CourseClazzMapper;
import com.tomehay.termprojectmanage_server.mapper.TeacherMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.Clazz;
import com.tomehay.termprojectmanage_server.pojo.Model.Course;
import com.tomehay.termprojectmanage_server.mapper.CourseMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.CourseClazz;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomehay.termprojectmanage_server.utils.IDWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomehay
 * @since 2023-06-20
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private ClazzMapper clazzMapper;

    @Resource
    private CourseClazzMapper courseClazzMapper;

    @Override
    public ResultVO<List<Course>> GetUnderGoCoursesByID(int teacherId) {
        List<Course> courses = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);
        queryWrapper.eq("course_status",1);
        courses = courseMapper.selectList(queryWrapper);
        return new ResultVO<>(ResponseCode.auto(courses),courses);
    }

    @Override
    public ResultVO<List<Course>> GetEndedCoursesByID(int teacherId) {
        List<Course> courses = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);
        queryWrapper.eq("course_status",2);
        courses = courseMapper.selectList(queryWrapper);
        return new ResultVO<>(ResponseCode.auto(courses),courses);
    }

    @Override
    public ResultVO<Void> CreateCourse(Course course) {
        IDWorker worker = new IDWorker(1,2,1);
        course.setCourseNo("1453"+String.valueOf(worker.nextId()));
        course.setCreateTime(LocalDateTime.now());
        course.setCourseStatus(1);
        int flag = courseMapper.insert(course);

        if (flag==-1){
            return new ResultVO<>(ResponseCode.errorOf("插入失败"));
        }

        return new ResultVO<>(ResponseCode.OK);
    }

    @Override
    public ResultVO<Void> DeleteCourse(int courseId) {
        Course course = courseMapper.selectById(courseId);

        if (course.getCourseStatus()!=2){
            return new ResultVO<>(ResponseCode.errorOf("该课程尚未结课，不能被删除"));
        }
        courseMapper.deleteById(courseId);

        return new ResultVO<>(ResponseCode.OK);
    }

    @Override
    public ResultVO<Void> EndCourse(int id) {
        Course course = courseMapper.selectById(id);
        course.setCourseStatus(2);
        if (updateById(course)){
            return new ResultVO<>(ResponseCode.OK);
        }
        return new ResultVO<>(ResponseCode.errorOf("结课失败"));
    }

    @Override
    public ResultVO<Course> GetOneCoursesByID(int id) {
        Course course = courseMapper.selectById(id);
        return new ResultVO<>(ResponseCode.auto(course),course);
    }

    @Override
    public ResultVO<List<Course>> GetUnderGoCoursesByStudentID(int id) {
        List<Course> courses = new ArrayList<>();
        courses = courseMapper.SelectByStudentID(id,1);
        return new ResultVO<>(ResponseCode.auto(courses),courses);
    }

    @Override
    public ResultVO<List<Course>> GetEndedCoursesByStudentID(int id) {
        List<Course> courses = new ArrayList<>();
        courses = courseMapper.SelectByStudentID(id,2);
        return new ResultVO<>(ResponseCode.auto(courses),courses);
    }

    @Override
    public ResultVO<List<Course>> getAllCourse(int id) {
        Clazz clazz = clazzMapper.selectOneById(id);
        List<CourseClazz> courseClazzes = courseClazzMapper.selectAllByClazzId(clazz.getId());
        List<Course> courses = new ArrayList<>();
        for (CourseClazz courseClazz: courseClazzes) {
            Course course = courseMapper.selectOneById(courseClazz.getCourseId());
            courses.add(course);
        }
        return ResultVO.createSuccessResponse(courses);
    }

}
